﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace TMS
{
    //Controls enum holding names of all our user controls

    public enum ControlsEnum
    {
        ADMIN_CONTROL,
        LOGIN_CONTROL,
        USERS_CONTROL,
        CREATE_EDIT_USER_CONTROL,
        PRODUCTS_CONTROL,
        CREATE_EDIT_PRODUCT_CONTROL,
        PROJECTS_CONTROL,
        CREATE_EDIT_PROJECT_CONTROL,
        EMPLOYEES_CONTROL,
        CREATE_EDIT_EMPLOYEE_CONTROL,
        MY_PROFILE_CONTROL,
        CREATE_EDIT_OFFERS_CONTROL,
        OFFERS_CONTROL,
        PRINT_OFFER_CONTROL,
        WAREHOUSE_CONTROL,
        STOCKTAKING_CONTROL,
        STOCKTAKING_LISTS_CONTROL,
        STOCKTAKING_PRODUCTS_CONTROL,
        CREATE_EDIT_STOCKTAKING_LISTS_CONTROL,
        STOCKTAKING_REPORT_CONTRO,
        INTERNAL_DISPATCHES_CONTROL,
		PARTNERS_CONTROL,
        CREATE_EDIT_PARTNER_CONTROL,
        RECEIVE_NOTE_CONTROL,
        CREATE_EDIT_RECEIVE_NOTE_CONTROL,
        CREATE_EDIT_INTERNAL_DISPATCH_NOTE,
        DISPATCH_NOTE_CONTROL,
        CREATE_EDIT_DISPATCH_NOTE_CONTROL,
        WORKORDER_CONTROL,
        CREATE_EDIT_WORKORDER_CONTROL,
        EDIT_WAREHOUSE_PRODUCTS_CONTROL,
        PRODUCT_CARD_CONTROL,
        VEHICLES_CONTROL,
        CREATE_EDIT_VEHICLES_CONTROL,
        ORDER_REQUEST_CONTROL,
        CREATE_EDIT_ORDER_REQUEST_CONTROL,
        AGREEMENTS_CONTROL,
        CREATE_EDIT_AGREEMENT_CONTROL
    }

    public partial class MainForm : Form
    {
        //Dictionary that holds all our instantiated user controls.
        private IDictionary<ControlsEnum, Control> controls = new Dictionary<ControlsEnum, Control>();

        Users ActiveUser;
        public DataSet OffresSet;
        public DataGridViewRow ParametarsMain;
        Product prod = new Product();
        //public static History history;

        public MainForm()
        {
            InitializeComponent();
            ActiveUser = Program.GetActiveUser;
            lblActiveUser.Text = ActiveUser.GetName()+" "+ActiveUser.GetSurname();

            this.WindowState = FormWindowState.Maximized;
        }

        public void ShowControl(ControlsEnum ctrl)
        {

            Control new_ctrl = null;
           
            new_ctrl = CreateControl(ctrl);
            controls[ctrl] = new_ctrl;

            new_ctrl.Parent = MainPanel;
            new_ctrl.Dock = DockStyle.Fill;
            new_ctrl.BringToFront();
            new_ctrl.Show();
        }

        public void ShowControl(ControlsEnum ctrl, DataGridViewRow parameters)
        {
            Control new_ctrl = null;

                new_ctrl = CreateControl(ctrl, parameters);
                controls[ctrl] = new_ctrl;

            new_ctrl.Parent = MainPanel;
            new_ctrl.Dock = DockStyle.Fill;
            new_ctrl.BringToFront();
            new_ctrl.Show();
        }

        private Control CreateControl(ControlsEnum ctrl)
        {
            switch (ctrl)
            {
                
                
                case ControlsEnum.USERS_CONTROL:
                    return new UsersControl();
                case ControlsEnum.PRODUCTS_CONTROL:
                    return new ProductsControl();
                case ControlsEnum.CREATE_EDIT_USER_CONTROL:
                    return new CreateEditUserControl();
                case ControlsEnum.CREATE_EDIT_PRODUCT_CONTROL:
                    return new CreateEditProductControl();
                case ControlsEnum.PROJECTS_CONTROL:
                    return new ProjectsControl();
                case ControlsEnum.CREATE_EDIT_PROJECT_CONTROL:
                    return new CreateEditProjectControl();
                case ControlsEnum.EMPLOYEES_CONTROL:
                    return new EmployeesControl();
                case ControlsEnum.CREATE_EDIT_EMPLOYEE_CONTROL:
                    return new CreateEditEmployeeControl();
                case ControlsEnum.MY_PROFILE_CONTROL:
                    return new MyProfileControl();
                case ControlsEnum.CREATE_EDIT_OFFERS_CONTROL:
                    return new CreateEditOfferControl();
                case ControlsEnum.PRINT_OFFER_CONTROL:
                    return new PrintOfferControl();
                case ControlsEnum.OFFERS_CONTROL:
                    return new OfferControl();
                case ControlsEnum.PARTNERS_CONTROL:
                    return new PartnersControl();
                case ControlsEnum.CREATE_EDIT_PARTNER_CONTROL:
                    return new CreateEditPartnerControl();
                case ControlsEnum.WAREHOUSE_CONTROL:
                    return new WarehouseControl();
                case ControlsEnum.STOCKTAKING_CONTROL:
                    return new StocktakingControl();
                case ControlsEnum.STOCKTAKING_LISTS_CONTROL:
                    return new StocktakingListsControl();
                case ControlsEnum.STOCKTAKING_PRODUCTS_CONTROL:
                    return new StocktakingProdutsControl();
                case ControlsEnum.CREATE_EDIT_STOCKTAKING_LISTS_CONTROL:
                    return new CreateEditStocktakingLists();
                case ControlsEnum.STOCKTAKING_REPORT_CONTRO:
                    return new StocktakingReportControl();
                case ControlsEnum.INTERNAL_DISPATCHES_CONTROL:
                    return new InternalDispatchNoteControl();
                case ControlsEnum.RECEIVE_NOTE_CONTROL:
                    return new ReceiveNoteControl();
                case ControlsEnum.CREATE_EDIT_RECEIVE_NOTE_CONTROL:
                    return new CreateEditReciveNoteControl();
                case ControlsEnum.CREATE_EDIT_INTERNAL_DISPATCH_NOTE:
                    return new CreateEditInternalDispatchNoteControl();
                case ControlsEnum.DISPATCH_NOTE_CONTROL:
                    return new DispatchNoteControl();
                case ControlsEnum.CREATE_EDIT_DISPATCH_NOTE_CONTROL:
                    return new CreateEditDispatchNoteControl();
                case ControlsEnum.WORKORDER_CONTROL:
                    return new WorkOrderControl();
                case ControlsEnum.CREATE_EDIT_WORKORDER_CONTROL:
                    return new CreateEditWorkOrderControl();
                case ControlsEnum.EDIT_WAREHOUSE_PRODUCTS_CONTROL:
                    return new EditWarehouseProductsControl();
                case ControlsEnum.PRODUCT_CARD_CONTROL:
                    return new ProductCardControl();
                case ControlsEnum.VEHICLES_CONTROL:
                    return new VehiclesControl();
                case ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL:
                    return new CreateEditVehicleControl();
                case ControlsEnum.ORDER_REQUEST_CONTROL:
                    return new OrderRequestControl();
                case ControlsEnum.CREATE_EDIT_ORDER_REQUEST_CONTROL:
                    return new CreateEditOrderRequestControl();
                case ControlsEnum.AGREEMENTS_CONTROL:
                    return new AgreementsControl();
                case ControlsEnum.CREATE_EDIT_AGREEMENT_CONTROL:
                    return new CreateEditAgreementControl();
                default:
                    return null;
            }
        }

        private Control CreateControl(ControlsEnum ctrl, DataGridViewRow parameters)
        {
            switch (ctrl)
            {
               
                
                case ControlsEnum.USERS_CONTROL:
                    return new UsersControl();
                case ControlsEnum.PRODUCTS_CONTROL:
                    return new ProductsControl(parameters);
                case ControlsEnum.CREATE_EDIT_USER_CONTROL:
                    return new CreateEditUserControl(parameters);
                case ControlsEnum.CREATE_EDIT_PRODUCT_CONTROL:
                    return new CreateEditProductControl(parameters);
                case ControlsEnum.PROJECTS_CONTROL:
                    return new ProjectsControl();
                case ControlsEnum.CREATE_EDIT_PROJECT_CONTROL:
                    return new CreateEditProjectControl(parameters);
                case ControlsEnum.EMPLOYEES_CONTROL:
                    return new EmployeesControl();
                case ControlsEnum.CREATE_EDIT_EMPLOYEE_CONTROL:
                    return new CreateEditEmployeeControl();
                case ControlsEnum.MY_PROFILE_CONTROL:
                    return new MyProfileControl();
                case ControlsEnum.CREATE_EDIT_OFFERS_CONTROL:
                    return new CreateEditOfferControl(parameters);
                case ControlsEnum.PRINT_OFFER_CONTROL:
                    return new PrintOfferControl(parameters);
                case ControlsEnum.OFFERS_CONTROL:
                    return new OfferControl(parameters);
                case ControlsEnum.PARTNERS_CONTROL:
                    return new PartnersControl();
                case ControlsEnum.CREATE_EDIT_PARTNER_CONTROL:
                    return new CreateEditPartnerControl(parameters);
                case ControlsEnum.WAREHOUSE_CONTROL:
                    return new WarehouseControl();
                case ControlsEnum.STOCKTAKING_CONTROL:
                    return new StocktakingControl();
                case ControlsEnum.STOCKTAKING_LISTS_CONTROL:
                    return new StocktakingListsControl(parameters);
                case ControlsEnum.STOCKTAKING_PRODUCTS_CONTROL:
                    return new StocktakingProdutsControl(parameters);
                case ControlsEnum.CREATE_EDIT_STOCKTAKING_LISTS_CONTROL:
                    return new CreateEditStocktakingLists(parameters);
                case ControlsEnum.STOCKTAKING_REPORT_CONTRO:
                    return new StocktakingReportControl(parameters);
                case ControlsEnum.INTERNAL_DISPATCHES_CONTROL:
                    return new InternalDispatchNoteControl();
                case ControlsEnum.RECEIVE_NOTE_CONTROL:
                    return new ReceiveNoteControl(parameters);
                case ControlsEnum.CREATE_EDIT_RECEIVE_NOTE_CONTROL:
                    return new CreateEditReciveNoteControl(parameters);
                case ControlsEnum.CREATE_EDIT_INTERNAL_DISPATCH_NOTE:
                    return new CreateEditInternalDispatchNoteControl(parameters);
                case ControlsEnum.DISPATCH_NOTE_CONTROL:
                    return new DispatchNoteControl();
                case ControlsEnum.CREATE_EDIT_DISPATCH_NOTE_CONTROL:
                    return new CreateEditDispatchNoteControl(parameters);
                case ControlsEnum.WORKORDER_CONTROL:
                    return new WorkOrderControl(parameters);
                case ControlsEnum.CREATE_EDIT_WORKORDER_CONTROL:
                    return new CreateEditWorkOrderControl(parameters);
                case ControlsEnum.EDIT_WAREHOUSE_PRODUCTS_CONTROL:
                    return new EditWarehouseProductsControl(parameters);
                case ControlsEnum.PRODUCT_CARD_CONTROL:
                    return new ProductCardControl();
                case ControlsEnum.VEHICLES_CONTROL:
                    return new VehiclesControl(parameters);
                case ControlsEnum.CREATE_EDIT_VEHICLES_CONTROL:
                    return new CreateEditVehicleControl(parameters);
                case ControlsEnum.ORDER_REQUEST_CONTROL:
                    return new OrderRequestControl(parameters);
                case ControlsEnum.CREATE_EDIT_ORDER_REQUEST_CONTROL:
                    return new CreateEditOrderRequestControl(parameters);
                case ControlsEnum.AGREEMENTS_CONTROL:
                    return new AgreementsControl(parameters);
                case ControlsEnum.CREATE_EDIT_AGREEMENT_CONTROL:
                    return new CreateEditAgreementControl(parameters);
                default:
                    return null;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (ActiveUser.EditUseres == false)
            {
                History.InitHistory(ControlsEnum.MY_PROFILE_CONTROL, null);
                ShowControl(ControlsEnum.MY_PROFILE_CONTROL);
            }
            else
            {
                History.InitHistory(ControlsEnum.USERS_CONTROL, null);
                ShowControl(ControlsEnum.USERS_CONTROL);
            }

        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
        }

        private void пописToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.STOCKTAKING_CONTROL,null);
            Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
        }

        private void прегледToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveUser.ViewWarehouse == true)
            {
                this.Cursor = Cursors.WaitCursor;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.WAREHOUSE_CONTROL, null);
                ShowControl(ControlsEnum.WAREHOUSE_CONTROL);
               
                this.Cursor = Cursors.Default;
            }
            else
            {
                MessageBox.Show("Немате привилегии за преглед на Магацинот");
            }
        }

        private void увозниПриемнициToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveUser.Finance == true)
            {
                DataGridView tmp = new DataGridView();
                tmp.RowCount = 1;
                tmp.ColumnCount = 1;
                tmp.Rows[0].Cells[0].Value = 1;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
                Program.GetMainForm.ShowControl(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
            }
            else
            {
                MessageBox.Show("Немате привилегии за преглед на Увозни приемници");
            }
            
        }

        private void приемнициToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveUser.Finance == true)
            {
                DataGridView tmp = new DataGridView();
                tmp.RowCount = 1;
                tmp.ColumnCount = 1;
                tmp.Rows[0].Cells[0].Value = 0;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
                Program.GetMainForm.ShowControl(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
            }
            else
            {
                MessageBox.Show("Немате привилегии за преглед на Приемници");
            }
            
        }

        private void испратнициToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.DISPATCH_NOTE_CONTROL,null);
            Program.GetMainForm.ShowControl(ControlsEnum.DISPATCH_NOTE_CONTROL);
        }

        private void интерниИспратнициToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.INTERNAL_DISPATCHES_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.INTERNAL_DISPATCHES_CONTROL);
        }

        private void работниНалозиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.WORKORDER_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.WORKORDER_CONTROL);
        }

        private void производиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveUser.ViewProducts == true)
            {
                this.Cursor = Cursors.WaitCursor;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.PRODUCTS_CONTROL, null);
                ShowControl(ControlsEnum.PRODUCTS_CONTROL);

                this.Cursor = Cursors.Default;
            }
            else
            {
                MessageBox.Show("Немате привилегии за преглед на Производи");
            }
        }

        
        private void новПроектToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveUser.EditProjects == true)
            {
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL, null);
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL);
            }
            else
                MessageBox.Show("Немате доволно привилегии за додавање на Проект");
        }


        private void прегледToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (ActiveUser.ViewProjects == true)
            {
                this.Cursor = Cursors.WaitCursor;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.PROJECTS_CONTROL, null);
                ShowControl(ControlsEnum.PROJECTS_CONTROL);

                this.Cursor = Cursors.Default;
            }
            else
            {
                MessageBox.Show("Немате привилегии за преглед на Проекти");
            }
        }

        private void прегледToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.PARTNERS_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.PARTNERS_CONTROL);
        }

        private void новКоминтентToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PARTNER_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PARTNER_CONTROL);
        }

        private void материјалнаКартицаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.PRODUCT_CARD_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.PRODUCT_CARD_CONTROL);
        }

        private void повратницаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridView tmp = new DataGridView();
            tmp.RowCount = 1;
            tmp.ColumnCount = 2;
            tmp.Rows[0].Cells[0].Value = 0;
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.RECEIVE_NOTE_CONTROL, tmp.Rows[0]);
            Program.GetMainForm.ShowControl(ControlsEnum.RECEIVE_NOTE_CONTROL,tmp.Rows[0]);
        }



        public void registerInHistoryMain(ControlsEnum ctrl, DataGridViewRow par)
        {
            History.registerInHistory(ctrl,par);
        }

        public bool Back() 
        {
            HistoryItem lastHistoryItem = History.PopBack();
            if (lastHistoryItem.parameters == null)
                Program.GetMainForm.ShowControl(lastHistoryItem.ctrl);
            else
                Program.GetMainForm.ShowControl(lastHistoryItem.ctrl, lastHistoryItem.parameters);
            
            return true;
        }

        public bool Forward()
        {
            try
            {
                HistoryItem lastHistoryItem = History.PopForward();
                if (lastHistoryItem.parameters == null)
                    Program.GetMainForm.ShowControl(lastHistoryItem.ctrl);
                else
                    Program.GetMainForm.ShowControl(lastHistoryItem.ctrl, lastHistoryItem.parameters);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void возилаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.VEHICLES_CONTROL, null);
            ShowControl(ControlsEnum.VEHICLES_CONTROL);
			this.Cursor = Cursors.Default;
        }

        private void корегирајToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
                MySqlDataAdapter MyDA = new MySqlDataAdapter();
                DataSet table = new DataSet();
                string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
                MySqlConnection mysqlCon = new MySqlConnection(connString);
                mysqlCon.Open();
                string sqlSelectAll = "SELECT * from dispatch_note_products WHERE Price<=0";
                MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);
                MyDA.Fill(table);
                MessageBox.Show(table.Tables[0].Rows.Count.ToString());
                foreach (DataRow row in table.Tables[0].Rows)
                {
                    Decimal Price = prod.GetAvgPriceFromWarehouse(Convert.ToInt32(row.ItemArray[1]));
                    MessageBox.Show(Price.ToString("N2"));
                    string sqlUpdate = "UPDATE dispatch_note_products SET Price=@Price WHERE ProductID=" + (Convert.ToInt32(row.ItemArray[1]));
                    MySqlCommand cmd = new MySqlCommand(sqlUpdate, mysqlCon);
                    cmd.Parameters.Add("@Price", MySqlDbType.Double, 50).Value = Price;

                    cmd.ExecuteNonQuery();

                }
                MessageBox.Show("Done");
            

           
            sqlSelectAll = "SELECT * from work_order_products WHERE Price<=0";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);
            MyDA.Fill(table);
            MessageBox.Show(table.Tables[0].Rows.Count.ToString());
            foreach (DataRow row in table.Tables[0].Rows)
            {
                Decimal Price = prod.GetAvgPriceFromWarehouse(Convert.ToInt32(row.ItemArray[1]));
                MessageBox.Show(Price.ToString("N2"));
                string sqlUpdate = "UPDATE work_order_products SET Price=@Price WHERE ProductID=" + (Convert.ToInt32(row.ItemArray[1]));
                MySqlCommand cmd = new MySqlCommand(sqlUpdate, mysqlCon);
                cmd.Parameters.Add("@Price", MySqlDbType.Double, 50).Value = Price;

                cmd.ExecuteNonQuery();

            }
            mysqlCon.Close();
            MessageBox.Show("Done");
        }

        private void вработениToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

    }
}
